<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Slide;
use Illuminate\Support\Facades\Input;
use DB;

class SlideController extends Controller
{
    

     //修改排序
    public function changeOrder()
    {   
       
       $input = Input::except('_token');
       // dd($input);

    
       $input['id'];
       $input['order'];
//       通过id找到要修改记录
       $slide = Slide::find($input['id']);
       $slide->order = $input['order'];
       $res = $slide->save();

       $data = [];
       if($res){
            $data['status'] = 0;
            $data['msg']  = '修改成功';
       }else{
           $data['status'] = 1;
           $data['msg']  = '修改失败';
       }
       return $data;
    }


    //    文件上传
    public function upload(Request $request)
    {

//        获取文件对象
        $file = Input::file('file_upload');
       // dd($file);
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;

//            1上传到本地服务器
            $path = $file->move(public_path().'/uploads',$newName);
//   
            $filepath = 'uploads/'.$newName;
            return  $filepath;
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        
        // 加载轮播图管理页面;
    //     $data = DB::table('sad_slide')->get();
    //     // dd($data);
    //     return view('admin.slide.index',['data'=>$data]);

        $count = $request -> input('count',5);
        $request = $request -> all();
        // 查询数据并分页
        $data = Slide::orderBy('order')->paginate($count);

        // dd($data);
        // 加载模版
        return view('admin.slide.index',['data'=>$data,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 加载 添加轮播图 页面
        return view('admin.slide.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         //获取到用户提交过来的数据
        $data = $request ->except('_token');
        // dd($data);
        // die;
        // 处理插入
        $slide = new Slide;
        $slide->pic = $data['pic'];
        $slide->url = $data['url'];
        $slide->order = $data['order'];
        $slide->description = $data['description'];
        
        // 判断 跳转页面

        if($slide->save()){
            return redirect('/admin/slide');
        }else{
            return back()->with('errors','添加失败');
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        
     
       $data = [];
       $aa = Slide::find($id);
       $res1 = $aa->delete();
       $res2 = \Storage::disk('public')->delete($aa->pic);
      
        //  删除成功
       if($res1 && $res2){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;

    }
}
